import requests
import json
import random
from settings import *

verify_num = 0
VERIFY_LOOP = 5
TIME_OUT = 8
VERIFY_HTTP_URL = 'http://icanhazip.com/'
VERIFY_HTTPS_URL = 'https://www.baidu.com/'

top_proxy_url = 'http://127.0.0.1:5000/https/top/1'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.3"
}

VERIFY_URL = {'HTTP': VERIFY_HTTP_URL, 'HTTPS': VERIFY_HTTPS_URL}

def verify(host, port, type):
    try:
        proxies = "%s://%s:%s" % (str(type), str(host), str(port))
        res = requests.get(url=VERIFY_URL[type], headers=headers, timeout=TIME_OUT, proxies={type: proxies})
        print('res.status_code  :  '+str(res.status_code))
        # if res.status_code == requests.codes.ok:
        #     return True
        # else:
        #     return False
        return True
    except:
        return False

def choice(ip_proxys):
    if len(ip_proxys) == 0:
        return None
    proxy = random.choice(ip_proxys)
    return str("%s://%s:%s" % (proxy['type'], proxy['host'], proxy['port']))
    # if verify(proxy['host'], proxy['port'], proxy['type']):
    #     return str("%s://%s:%s" % (proxy['type'], proxy['host'], proxy['port']))
    # ip_proxys.remove(proxy)
    # return choice(ip_proxys)



def get():
    global verify_num
    verify_num = verify_num + 1
    res = requests.get(top_proxy_url, headers=headers)
    ip_porxys = json.loads(res.text)
    proxy = choice(ip_porxys)
    if proxy is None:
        return get()
    verify_num = 0
    return proxy


import time

last_time = 0
last_proxy = None

def getData5uProxy():
    global last_time
    global last_proxy
    cur_time = time.time()
    if int(cur_time) - int(last_time) > 10 or last_time==0:
        last_time = cur_time
        # url = 'http://api.ip.data5u.com/dynamic/get.html?order=f9ae0d4529fad58993ec0d27a4b74baf&random=1&sep=3'
        url = 'https://api.xiaoxiangdaili.com/ip/get?appKey=619441638909300736&appSecret=QD5iCje5&cnt=&wt=text'
        res = requests.get(url, headers=headers).text
        last_proxy = res
        return res
    else:
        return last_proxy






if __name__ == '__main__':

    while True:
        time.sleep(1)
        print(getData5uProxy())